package edu.umn.cs5115.scheduler.search;

import edu.umn.cs5115.scheduler.entities.Weekday;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.GridData;

import edu.umn.cs5115.scheduler.entities.Course;
import edu.umn.cs5115.scheduler.entities.MeetingTime;
import edu.umn.cs5115.scheduler.entities.Section;
import edu.umn.cs5115.scheduler.framework.*;

public class CourseInfo {
	private StyledText text;
	private GridData data;
	
	public CourseInfo(StyledText _text, GridData _data)
	{
		text=_text;
		data=_data;
	}
	
	public void setGridData()
	{
		text.setLayoutData(data);
	}
	
        /**
         * Sets the text directly.
         * @param _text The new value of the text.
         */
	public void setText(String _text)
	{
		text.setText(_text);
	}
	
        /**
         * Sets the text by formatting a course.
         * @param _course The course to retrieve information from and format.
         */
	public void setText(Course _course)
	{
		SimpleDateFormat formatter = new SimpleDateFormat();
		formatter.applyPattern("hh:mm a");
		
		//create style ranges
		StyleRange header = new StyleRange();

		//get header information
		StringBuilder info = new StringBuilder();
		info.append(_course.getDepartment());
		info.append(" ");
		info.append(_course.getCourseNumber());
		info.append(" ");
		info.append(_course.getName());
		info.append("\n");
		header.start = 0;
		header.length = info.length();
		
		//add course prereqs
		info.append(_course.getPrereqs());
		info.append("\n");
		
		//add course libeds
		info.append("Lib Eds: ");
		info.append(_course.getLibEds());
		info.append("\n\n");
		
		//add course description
        if (_course.getDescription() != null && _course.getDescription() !="null")
        {
            info.append(_course.getDescription());
            info.append("\n\n");
        }
        else
        {
        	info.append("Student may contact the instructor or department for information");
        	info.append("\n\n");
        }
		
		//iterate through sections and subsections.
		Iterator<Section> iter = _course.getSections().iterator();
		while( iter.hasNext() ) {
			Section section = iter.next();
			info.append(section.getSectionNumber());
			info.append(" ");
			info.append(typeLookup(section.getType()));
			info.append(" ");
			
			//append the meeting time(s)
			Set<MeetingTime> times = section.getMeetingTimes();
			Iterator<MeetingTime> timeIter = times.iterator();
			if ( times.size() > 1 )
				info.append(" ");
			while ( timeIter.hasNext() ) 
			{
				MeetingTime mt = timeIter.next();
                                if (mt.getStartTime() != null && mt.getEndTime() != null)
                                {
                                    info.append(formatter.format(mt.getStartTime()));
                                    info.append(" - ");
                                    info.append(formatter.format(mt.getEndTime()));
                                    info.append(" ");
                                }
				
				// append the days of the week        e.g.  M,W,F
				Iterator<Weekday> dayIter = mt.getMeetingDays().iterator();
				while (dayIter.hasNext()) {
					info.append(dayIter.next());
					if ( dayIter.hasNext() ) info.append(", ");
				}
				//append locale load
				info.append("\n");
				info.append(mt.getLocation());
			}
			info.append("\n");
			//append instructor
			info.append(section.getStaff());
			//append credit
			if(section.getCredits() > 0)
				info.append("\n\n" + section.getCredits() + " Credits");
			info.append("\n\n");
			
			if ( section.getSubSections().size() > 0 ) 
			{
				Iterator<Section> iter2 = section.getSubSections().iterator();
				while( iter2.hasNext() )
				{
					Section subsection = iter2.next();
					info.append("\t");
					info.append(subsection.getSectionNumber());
					info.append(" ");
					info.append(typeLookup(subsection.getType()));
					info.append(" ");
					
					//append the meeting time(s)
					Set<MeetingTime> subTimes = subsection.getMeetingTimes();
					Iterator<MeetingTime> subTimeIter = subTimes.iterator();
					if ( subTimes.size() > 1 )
						info.append(" ");
					while ( subTimeIter.hasNext() ) 
					{
						MeetingTime mt = subTimeIter.next();
						info.append(formatter.format(mt.getStartTime()));
						info.append(" - ");
						info.append(formatter.format(mt.getEndTime()));
						info.append(" ");
						
						// append the days of the week        e.g.  M,W,F
						Iterator<Weekday> dayIter = mt.getMeetingDays().iterator();
						while (dayIter.hasNext()) {
							info.append(dayIter.next());
							if ( dayIter.hasNext() ) info.append(", ");
						}
						//append locale load
						info.append("\n\t");
						info.append(mt.getLocation());
					}
					info.append("\n\t");
					//append instructor
					info.append(subsection.getStaff());
					//append credit
					if(subsection.getCredits() > 0)
						info.append("\n\t" + subsection.getCredits() + " Credits");
					info.append("\n\n");
				}
			}
		}
		text.setText(info.toString());
		
		//much formatting!
		
		header.fontStyle = SWT.BOLD;
		text.setStyleRange(header);
	}
	
        /**
         * Formats and displays text for a section.
         * @param _section The section to format text for.
         */
	public void setText(Section _section)
	{
		
		Course course = _section.getCourse();
		SimpleDateFormat formatter = new SimpleDateFormat();
		formatter.applyPattern("hh:mm a");
	
		//create style ranges
		StyleRange header = new StyleRange();
		header.fontStyle = SWT.BOLD;
		StyleRange preReqs = new StyleRange();
		preReqs.fontStyle = SWT.BOLD;
		StyleRange libEds = new StyleRange();
		libEds.fontStyle = SWT.BOLD;
		StyleRange instructor = new StyleRange();
		instructor.fontStyle = SWT.BOLD;
		StyleRange description = new StyleRange();
		description.fontStyle = SWT.BOLD;
		StyleRange credits = new StyleRange();
		credits.fontStyle = SWT.BOLD;

		//get header information
		header.start = 0;
		StringBuilder info = new StringBuilder();
		info.append(course.getDepartment());
		info.append(" ");
		info.append(course.getCourseNumber());
		info.append(" ");
		info.append(course.getName());
		header.length = info.length();
		info.append("\n");
		
		//append the meeting time(s)
		Set<MeetingTime> times = _section.getMeetingTimes();
		Iterator<MeetingTime> timeIter = times.iterator();
		if ( times.size() > 1 )
			info.append(" ");
		while ( timeIter.hasNext() ) 
		{
			MeetingTime mt = timeIter.next();
                            if (mt.getStartTime() != null && mt.getEndTime() != null)
                            {
                                info.append(formatter.format(mt.getStartTime()));
                                info.append(" - ");
                                info.append(formatter.format(mt.getEndTime()));
                                info.append(" ");
                            }
			
			// append the days of the week        e.g.  M,W,F
			Iterator<Weekday> dayIter = mt.getMeetingDays().iterator();
			while (dayIter.hasNext()) {
				info.append(dayIter.next());
				if ( dayIter.hasNext() ) info.append(", ");
			}
			//append locale load
			info.append("\n");
			info.append(mt.getLocation());
			info.append("\n");
		}
		
		//add credit amount
		if(_section.getCredits() > 0)
		{
			credits.start = info.length();
			info.append("Credits: ");
			credits.length = info.length() - credits.start;
			info.append(_section.getCredits());
			info.append("\n");
		}
		
		//add course prereqs
		preReqs.start = info.length();
		info.append("Prerequisites: ");
		preReqs.length = info.length() - preReqs.start;
		info.append(course.getPrereqs());
		info.append("\n");
		
		//add course libeds
		libEds.start = info.length();
		info.append("Lib Eds: ");
		libEds.length = info.length() - libEds.start;
		info.append(course.getLibEds());
		info.append("\n");
		
		//append instructor
		instructor.start = info.length();
		info.append("Instructor: ");
		instructor.length = info.length() - instructor.start;
		info.append(_section.getStaff());
		info.append("\n");
		
		//append description
		description.start = info.length();
		info.append("Description: ");
		description.length = info.length() - description.start;
		info.append(course.getDescription());
		info.append("\n");
		
		//set the text!
		text.setText(info.toString());
		text.setStyleRange(header);
		text.setStyleRange(preReqs);
		text.setStyleRange(credits);
		text.setStyleRange(instructor);
		text.setStyleRange(description);
		text.setStyleRange(libEds);
	}
	
	private String typeLookup(String type)
	{
		if (type == "LEC")
			return "Lecture";
		else if( type == "DIS")
			return "Discussion";
		else if(type == "LAB")
			return "Lab";
		else if (type == "IND")
			return "Independent";
		else
			return type;
	}
}
